package com.hxd.mall.module.product.search

import android.graphics.Color
import android.view.KeyEvent
import android.view.View
import android.view.inputmethod.EditorInfo.IME_ACTION_SEARCH
import android.widget.TextView
import com.alibaba.android.arouter.facade.annotation.Route
import com.hxd.mall.R
import com.hxd.mall.base.BaseInjectActivity
import com.hxd.mall.constant.HxdConstants.IntentParamsKeyDefine.search_Key
import com.hxd.mall.constant.HxdConstantsRouterPath.Search.path_product_search
import com.hxd.mall.module.product.search.history.SearchHistoryFragment
import com.hxd.mall.module.product.search.result.SearchResultFragment
import com.hxd.mall.utils.local.StackManageUtil.removeActivity
import com.hxd.mall.utils.local.search_page_button_click
import com.hxd.mall.utils.local.search_page_view
import com.hxd.mall.utils.local.sendPoint
import com.hxd.mall.utils.open.ToastUtil
import com.hxd.mall.utils.open.UIUtil.hideKeyboard
import com.hxd.mall.utils.open.UIUtil.shKeyboard
import com.hxd.mall.utils.open.addSetOnClickListener
import com.hxd.mall.utils.open.addTextWatcherListener
import crossoverone.statuslib.StatusUtil.setSystemStatus
import crossoverone.statuslib.StatusUtil.setUseStatusBarColor
import kotlinx.android.synthetic.main.activity_search_layout.*

/**
 * 搜索页
 */
@Route(path = path_product_search)
class SearchActivity : BaseInjectActivity<ISearchPresenter>(), ISearchView {

    override fun attachLayoutRes(): Int = R.layout.activity_search_layout

    override fun initViews() {
        showHistoryFragment()

        addSetOnClickListener(search, iv_back, clear) {
            onClick(it)
        }

        addTextWatcherListener(keyword) {
            clear.visibility = if (it.isNotEmpty()) View.VISIBLE else View.GONE
        }

        //如果是从粘贴搬进来的则直接搜索
        val searchContent = intent.getStringExtra(search_Key) ?: ""
        if (searchContent.isNotEmpty()) {
            toSearch(searchContent)
        }

        keyword.setOnEditorActionListener(object : TextView.OnEditorActionListener {
            override fun onEditorAction(p0: TextView?, p1: Int, p2: KeyEvent?): Boolean {
                if (p1 == IME_ACTION_SEARCH) {
                    //关闭软键盘
                    p0?.let { hideKeyboard(it) }
                    keyword.text?.toString()?.trim()?.let {
                        toSearch(it)
                    }
                    return true
                }
                return false
            }
        })

        sendPoint(search_page_view)

        keyword.postDelayed({
            shKeyboard(keyword)
        }, 300)
    }

    fun searchText() = keyword.text.toString()

    //接收从baseActivity传进来的参数
    override fun toSearch(searchText: String) {
        keyword.setText(searchText)
        keyword.setSelection(searchText.length)
        showSearchRecord()
    }

    private fun onClick(it: View) {
        when (it) {
            search -> {
                sendPoint(search_page_button_click)
                if (keyword.text.toString().isNotEmpty()) {
                    showSearchRecord()
                } else {
                    ToastUtil.showToast("请输入要搜索的内容")
                }
            }
            iv_back -> {
                hideKeyboard(keyword)
                removeActivity(this)
            }
            clear -> {
                keyword.setText("")
            }
        }
    }

    override fun initData() {
    }

    override fun showData() {
    }

    override fun setStatusColor() {
        setUseStatusBarColor(this, Color.WHITE)
    }

    override fun setSystemInvadeBlack() {
        setSystemStatus(this, false, true)
    }

    private fun showHistoryFragment() {
        supportFragmentManager.beginTransaction().run {
            add(R.id.content, SearchHistoryFragment())
            commit()
        }
    }

    private fun showSearchRecord() {
        val truncation = supportFragmentManager.beginTransaction()
        truncation.run {
            replace(R.id.content, SearchResultFragment())
            addToBackStack(SearchResultFragment::class.java.name)
            commit()
        }
    }

    override fun onBackPressed() {
        super.onBackPressed()
        removeActivity(this)
    }

    override fun onStop() {
        super.onStop()
        hideKeyboard(keyword)
    }


}
